package com.swordxu.yim.server.utils;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;

/**
 * 对象序列化和反序列化
 * Created by user063 on 2015/1/5.
 */
public class KryoRedisSerializer<T> {
    private static Kryo kryo = new Kryo();

    public static byte[] serialize(Object obj) {
        byte[] buffer = new byte[2048];
        Output output = new Output(buffer);
        kryo.writeClassAndObject(output, obj);
        return output.toBytes();
    }

    public static Object deSerialize(byte[] bytes) {
        if (bytes == null) {
            return null;
        }
        Input input = new Input(bytes);
        return kryo.readClassAndObject(input);
    }
}
